package com.migu.rbac.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 2018/02/24
 */
public final class DateTimeUtil {

    private static final String yyyyMMddHHmmssSSS2 = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String yyyyMMdd_HH_mm_ss = "yyyyMMdd HH:mm:ss";
    private static final String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    private static final String yyyyMMddHHmmss2 = "yyyyMMddHHmmss";
    private static final String yyyyMMddTHHmmssSSSZ = "yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'";
    private static final String yyyyMMddTHHmmssSSS = "yyyy-MM-dd'T'HH:mm:ss'.'SSS";
    private static final String YYYY_MM_DD = "yyyy-MM-dd";
    private static final String YYYY = "yyyy";
    private static final String MM = "MM";
    private static final String DD = "dd";

    private static Logger logger = LoggerFactory.getLogger(DateTimeUtil.class);

    private DateTimeUtil() {
    }


    //日期转换为字符串
    public static String formatTime(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    //字符串转换为日期
    public static Date toTime(String time, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(time);
        } catch (ParseException e) {
            logger.error("parse string to date error.", e.getMessage());
        }
        return null;
    }

    /**
     * 将日期字符串(yyyy-MM-dd HH:mm:ss SSS)转换为yyyyMMddHHmmssSSS
     *
     * @param dateStr
     * @return
     */
    public static String formatDateString3(String dateStr) throws Exception {
        Date date = toTime(dateStr, DateTimeUtil.yyyyMMddHHmmss);
        return formatTime(date, DateTimeUtil.yyyyMMddHHmmss2);
    }

    /**
     * 将日期字符串(yyyy-MM-dd HH:mm:ss SSS)转换为yyyyMMddHHmmssSSS
     *
     * @param dateStr
     * @return
     */
    public static String formatDateString2(String dateStr) throws Exception {
        Date date = toTime(dateStr, DateTimeUtil.yyyyMMddHHmmssSSS2);
        return formatTime(date, DateTimeUtil.yyyyMMddHHmmssSSS);
    }

    /**
     * 将日期字符串(yyyy-MM-dd HH:mm:ss)转换为yyyyMMddHHmmssSSS
     *
     * @param dateStr
     * @return
     */
    public static String formatDateString(String dateStr) throws Exception {
        Date date = toTime(dateStr, DateTimeUtil.yyyyMMddHHmmss);
        return formatTime(date, DateTimeUtil.yyyyMMddHHmmss2) + "000";
    }

    public static String formatDateUTCString(long dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyyMMddHHmmssSSS2);
        Date date = new Date(dateStr);
        SimpleDateFormat df = new SimpleDateFormat(yyyyMMddTHHmmssSSSZ);
        return df.format(date);
    }

    /**
     * 获取当前时间的yyyy-MM-dd
     *
     * @return
     */
    public static String getDateShort() {
        return formatTime(new Date(), DateTimeUtil.YYYY_MM_DD);
    }

    public static String getYear() {
        return formatTime(new Date(), DateTimeUtil.YYYY);
    }

    public static String getMonth() {
        return formatTime(new Date(), DateTimeUtil.MM);
    }

    public static String getDay() {
        return formatTime(new Date(), DateTimeUtil.DD);
    }

    public static String formatDateUTCString(String dateStr) {
        String dateShort = getDateShort();
        SimpleDateFormat yymmddhhmmss = new SimpleDateFormat(yyyyMMddHHmmss);
        Date parse = null;
        try {
            parse = yymmddhhmmss.parse(dateShort + " " + dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat df = new SimpleDateFormat(yyyyMMddTHHmmssSSS);
        return df.format(parse) + "+08:00";
    }

    //获取当前时间-前五分钟
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String format = sdf.format(new Date());
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String time = sdf.format(beforeD);
        return time + "～" + format;
    }

    /**
     * 前五分钟
     * @param strData
     * @return
     */
    public static String getPreDateByDate(String strData) {
        String preDate = "";
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(strData);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int min = c.get(Calendar.MINUTE);
        c.set(Calendar.MINUTE, min - 5);
        preDate = sdf.format(c.getTime());
        return preDate;
    }

}
